package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.pojo.CheckItem;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("checkItem")
public class CheckItemController {
    //远程调用服务
    @Reference
    private CheckItemService checkItemService;


    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckItem byId = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,byId);

        }catch(Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.findPage(queryPageBean);

        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);

    }


    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);

        }catch(Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    @RequestMapping("/AddCheckItem")
    public Result AddCheckItem(@RequestBody CheckItem checkItem){
        try{
            Integer row = checkItemService.AddCheckItem(checkItem);
            return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,row);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
    }

    @RequestMapping("/DelCheckItem")
    public Result DelCheckItem(Integer id) throws CustomException {
        Integer row = checkItemService.DelCheckItem(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS,row);
    }

    @RequestMapping("/UpdCheckItem")
    public Result UpdCheckItem(@RequestBody CheckItem checkItem){
        try{
            Integer row = checkItemService.UpdCheckItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,row);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
    }
}
